<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人资料 - 管理员端</title>
    <link rel="stylesheet" href="../css/common.css">
    <style>
        .profile-container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        .profile-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .profile-avatar {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 48px;
            color: white;
            margin: 0 auto 20px;
            cursor: pointer;
            transition: transform 0.3s ease;
        }

        .profile-avatar:hover {
            transform: scale(1.05);
        }

        .profile-avatar img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            object-fit: cover;
        }

        .profile-tabs {
            display: flex;
            border-bottom: 2px solid #e9ecef;
            margin-bottom: 30px;
        }

        .profile-tab {
            padding: 12px 24px;
            background: none;
            border: none;
            cursor: pointer;
            font-size: 16px;
            color: #6c757d;
            border-bottom: 2px solid transparent;
            transition: all 0.3s ease;
        }

        .profile-tab.active {
            color: #007bff;
            border-bottom-color: #007bff;
        }

        .profile-tab:hover {
            color: #007bff;
        }

        .profile-content {
            background: white;
            border-radius: 12px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
        }

        .form-group input,
        .form-group select {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }

        .form-group input:focus,
        .form-group select:focus {
            outline: none;
            border-color: #007bff;
        }

        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }

        .btn-save {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.3s ease;
        }

        .btn-save:hover {
            transform: translateY(-2px);
        }

        .btn-save:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .password-form {
            display: none;
        }

        .password-form.active {
            display: block;
        }

        .info-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 0;
            border-bottom: 1px solid #e9ecef;
        }

        .info-item:last-child {
            border-bottom: none;
        }

        .info-label {
            font-weight: 600;
            color: #333;
        }

        .info-value {
            color: #6c757d;
        }

        .role-badge {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
        }

        .role-admin {
            background: #dc3545;
            color: white;
        }

        .role-teacher {
            background: #28a745;
            color: white;
        }

        .role-student {
            background: #007bff;
            color: white;
        }

        .status-badge {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
        }

        .status-active {
            background: #28a745;
            color: white;
        }

        .status-inactive {
            background: #6c757d;
            color: white;
        }

        @media (max-width: 768px) {
            .form-row {
                grid-template-columns: 1fr;
            }
            
            .profile-tabs {
                flex-wrap: wrap;
            }
            
            .profile-tab {
                flex: 1;
                min-width: 120px;
            }
        }

        .sidebar-footer {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            padding: 20px;
            border-top: 1px solid var(--border-color);
        }

        .logout-btn {
            width: 100%;
            padding: 12px 16px;
            background: var(--error-color);
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .logout-btn:hover {
            background: #c82333;
            transform: translateY(-1px);
        }
    </style>
</head>
<body>
    <div class="main-content">
        <!-- 侧边栏 -->
        <div class="sidebar">
            <div class="sidebar-header">
                <h3>管理员工作台</h3>
            </div>
            <ul class="sidebar-nav">
                <li class="nav-item">
                    <a href="dashboard.html" class="nav-link">
                        <i>📊</i> 仪表盘
                    </a>
                </li>
                <li class="nav-item">
                    <a href="users.html" class="nav-link">
                        <i>👥</i> 用户管理
                    </a>
                </li>
                <li class="nav-item">
                    <a href="videos.html" class="nav-link">
                        <i>🎥</i> 视频管理
                    </a>
                </li>
                <li class="nav-item">
                    <a href="pending-videos.html" class="nav-link">
                        <i>📹</i> 待审核视频
                    </a>
                </li>
                <li class="nav-item">
                    <a href="exam-management.html" class="nav-link">
                        <i>📝</i> 考试管理
                    </a>
                </li>
                <li class="nav-item">
                    <a href="profile.html" class="nav-link active">
                        <i>👤</i> 个人资料
                    </a>
                </li>
            </ul>
            
            <!-- 退出登录 -->
            <div class="sidebar-footer">
                <button class="logout-btn" onclick="UserManager.logout()">
                    <i>🚪</i> 退出登录
                </button>
            </div>
        </div>

        <!-- 主内容区 -->
        <div class="content-wrapper">
            <div class="content-header">
                <h1>个人资料</h1>
                <p>管理您的个人信息和账户设置</p>
            </div>

            <div class="profile-container">
                <div class="profile-header">
                    <div class="profile-avatar" onclick="changeAvatar()">
                        <span id="avatarText">👤</span>
                    </div>
                    <h2 id="userName">加载中...</h2>
                    <p id="userRole">管理员</p>
                </div>

                <div class="profile-tabs">
                    <button class="profile-tab active" onclick="showTab('profile')">基本信息</button>
                    <button class="profile-tab" onclick="showTab('password')">修改密码</button>
                </div>

                <!-- 基本信息 -->
                <div id="profileContent" class="profile-content">
                    <form id="profileForm">
                        <div class="form-row">
                            <div class="form-group">
                                <label for="username">用户名</label>
                                <input type="text" id="username" readonly>
                            </div>
                            <div class="form-group">
                                <label for="name">姓名</label>
                                <input type="text" id="name" required>
                            </div>
                        </div>

                        <div class="form-row">
                            <div class="form-group">
                                <label for="email">邮箱</label>
                                <input type="email" id="email">
                            </div>
                            <div class="form-group">
                                <label for="phone">手机号</label>
                                <input type="tel" id="phone">
                            </div>
                        </div>

                        <div class="form-group">
                            <label>账户信息</label>
                            <div class="info-item">
                                <span class="info-label">角色</span>
                                <span id="roleBadge" class="role-badge role-admin">管理员</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">状态</span>
                                <span id="statusBadge" class="status-badge status-active">正常</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">注册时间</span>
                                <span id="createTime" class="info-value">-</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">最后登录</span>
                                <span id="lastLoginTime" class="info-value">-</span>
                            </div>
                        </div>

                        <button type="submit" class="btn-save">保存修改</button>
                    </form>
                </div>

                <!-- 修改密码 -->
                <div id="passwordContent" class="profile-content password-form">
                    <form id="passwordForm">
                        <div class="form-group">
                            <label for="oldPassword">原密码</label>
                            <input type="password" id="oldPassword" required>
                        </div>

                        <div class="form-group">
                            <label for="newPassword">新密码</label>
                            <input type="password" id="newPassword" required minlength="6">
                        </div>

                        <div class="form-group">
                            <label for="confirmPassword">确认新密码</label>
                            <input type="password" id="confirmPassword" required minlength="6">
                        </div>

                        <button type="submit" class="btn-save">修改密码</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script src="../js/common.js"></script>
    <script>
        let currentUser = null;

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            checkAuth();
            loadUserProfile();
        });

        // 检查用户认证
        function checkAuth() {
            if (!UserManager.isLoggedIn()) {
                Message.error('请先登录');
                window.location.href = '../login.html';
                return;
            }
        }

        // 加载用户资料
        async function loadUserProfile() {
            try {
                const response = await API.get('/user/profile');
                if (response.success) {
                    currentUser = response.user;
                    renderUserProfile();
                } else {
                    Message.error(response.message || '加载用户资料失败');
                }
            } catch (error) {
                console.error('加载用户资料失败:', error);
                Message.error('加载用户资料失败，请稍后重试');
            }
        }

        // 渲染用户资料
        function renderUserProfile() {
            if (!currentUser) return;

            // 基本信息
            document.getElementById('userName').textContent = currentUser.name || currentUser.username;
            document.getElementById('username').value = currentUser.username || '';
            document.getElementById('name').value = currentUser.name || '';
            document.getElementById('email').value = currentUser.email || '';
            document.getElementById('phone').value = currentUser.phone || '';

            // 角色信息
            const roleNames = { 0: '管理员', 1: '老师', 2: '学生' };
            const roleColors = { 0: 'role-admin', 1: 'role-teacher', 2: 'role-student' };
            document.getElementById('userRole').textContent = roleNames[currentUser.role] || '未知';
            
            const roleBadge = document.getElementById('roleBadge');
            roleBadge.textContent = roleNames[currentUser.role] || '未知';
            roleBadge.className = `role-badge ${roleColors[currentUser.role] || 'role-admin'}`;

            // 状态信息
            const statusBadge = document.getElementById('statusBadge');
            if (currentUser.status === 1) {
                statusBadge.textContent = '正常';
                statusBadge.className = 'status-badge status-active';
            } else {
                statusBadge.textContent = '禁用';
                statusBadge.className = 'status-badge status-inactive';
            }

            // 时间信息
            document.getElementById('createTime').textContent = formatDate(currentUser.createTime);
            document.getElementById('lastLoginTime').textContent = formatDate(currentUser.lastLoginTime);

            // 头像
            updateAvatar();
        }

        // 更新头像显示
        function updateAvatar() {
            const avatarText = document.getElementById('avatarText');
            if (currentUser && currentUser.avatar) {
                avatarText.innerHTML = `<img src="${currentUser.avatar}" alt="头像">`;
            } else {
                avatarText.textContent = currentUser ? (currentUser.name ? currentUser.name.charAt(0) : '👤') : '👤';
            }
        }

        // 显示标签页
        function showTab(tabName) {
            // 更新标签按钮状态
            document.querySelectorAll('.profile-tab').forEach(tab => {
                tab.classList.remove('active');
            });
            event.target.classList.add('active');

            // 显示对应内容
            document.getElementById('profileContent').style.display = tabName === 'profile' ? 'block' : 'none';
            document.getElementById('passwordContent').style.display = tabName === 'password' ? 'block' : 'none';
        }

        // 修改头像
        function changeAvatar() {
            Message.info('头像上传功能开发中...');
        }

        // 保存基本信息
        document.getElementById('profileForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const formData = {
                name: document.getElementById('name').value,
                email: document.getElementById('email').value,
                phone: document.getElementById('phone').value
            };

            try {
                const response = await API.put('/user/profile', formData);
                if (response.success) {
                    Message.success('个人资料更新成功');
                    currentUser = response.user;
                    renderUserProfile();
                } else {
                    Message.error(response.message || '更新失败');
                }
            } catch (error) {
                console.error('更新个人资料失败:', error);
                Message.error('更新失败，请稍后重试');
            }
        });

        // 修改密码
        document.getElementById('passwordForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const oldPassword = document.getElementById('oldPassword').value;
            const newPassword = document.getElementById('newPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (newPassword !== confirmPassword) {
                Message.error('新密码和确认密码不一致');
                return;
            }

            if (newPassword.length < 6) {
                Message.error('新密码长度不能少于6位');
                return;
            }

            try {
                const response = await API.put('/user/change-password', {
                    oldPassword: oldPassword,
                    newPassword: newPassword,
                    confirmPassword: confirmPassword
                });
                
                if (response.success) {
                    Message.success('密码修改成功');
                    document.getElementById('passwordForm').reset();
                } else {
                    Message.error(response.message || '密码修改失败');
                }
            } catch (error) {
                console.error('修改密码失败:', error);
                Message.error('修改密码失败，请稍后重试');
            }
        });

        // 格式化日期
        function formatDate(dateString) {
            if (!dateString) return '-';
            const date = new Date(dateString);
            return date.toLocaleString('zh-CN');
        }

        // 退出登录函数已移至UserManager.logout()，无需重复定义
    </script>
</body>
</html>
